package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.common.constant.SystemConstant;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.sysconfig.bean.Menu;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.query.MenuQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by justin on 15/7/29.
 */
@Service
public class MenuService extends BaseServiceImpl<Menu> {
    @Autowired
    SystemLogService systemLogService;

    public boolean saveMenu(Menu menu){

        if(menu.getId() == null){
            if(menu.getParentId()!=null){
                //添加子菜单
                if(menu.getParentId()==0){
                    menu.setLevel(2);
                }else{
                    Menu parentMenu = get(menu.getParentId());
                    menu.setLevel(parentMenu.getLevel()+1);
                }

            }else {
                menu.setParentId(0);
                menu.setLevel(1);
            }
            insert(menu);
            systemLogService.log(SystemLog.ModuleTypeEnum.MENULIST, SystemLog.OperateTypeEnum.ADD, menu.getId());

        }else{
            update(menu);
            systemLogService.log(SystemLog.ModuleTypeEnum.MENULIST, SystemLog.OperateTypeEnum.EDIT, menu.getId());

        }
        refeshMenus();
        return true;
    }

    public Paged<Menu> listPagedMenus(MenuQuery menuQuery) {
        Query<Menu> query = Query.build(Menu.class);

        query.setPaged(menuQuery.getPageNo(), menuQuery.getPageSize());
        Paged<Menu> pagedMenus = findPagedByQuery(query);
        return pagedMenus;
    }

    public Menu getParentInfo(Integer parentId) {
        Menu menu = new Menu();
        Menu parentMenu = get(parentId);
        menu.setParentId(parentId);
        menu.setParentName(parentMenu.getName());
        return menu;
    }

    public List<Menu> listMenus(MenuQuery menuQuery) {
        Query<Menu> query = Query.build(Menu.class);
        List<Menu> menus = findByQuery(query);
        return treeSortMenus(menus);
    }

    private List<Menu> treeSortMenus(List<Menu> menus) {
        List<Menu> topMenus = new ArrayList<>();
        Map<Integer,List<Menu>> subMap = new HashMap<>();
        List<Menu> menuList = null;
        for(Menu menu : menus){
            menuList = subMap.get(menu.getParentId());
            if(menuList==null){
                menuList = new ArrayList<>();
            }
            menuList.add(menu);
            subMap.put(menu.getParentId(),menuList);
        }
        topMenus = subMap.get(0);
        if(topMenus!=null){
            Collections.sort(topMenus);
            addSortedSub(subMap,topMenus);
        }

        return topMenus;
    }

    private void addSortedSub(Map<Integer,List<Menu>> subMap,List<Menu> fatherMenus){
        List<Menu> menuList = null;
        for(Menu menu :fatherMenus){
            menuList = subMap.get(menu.getId());
            if(menuList!=null){
                Collections.sort(menuList);
                menu.setSubMenuList(menuList);
                addSortedSub(subMap, menuList);
            }
        }
    }

    public  void  refeshMenus(){
        MenuQuery query = new MenuQuery();
        SystemConstant.menus = this.listMenus(query);
    }
}
